package redis

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

var pool *redis.Pool

func Init() {
	pool = &redis.Pool{
		MaxIdle:     16,
		MaxActive:   1024,
		IdleTimeout: 300,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", "localhost:6379")
		},
	}
}
func SetRedis(bytes []byte, key string, reqDur int) {
	c := pool.Get()
	defer c.Close()
	_, err := c.Do("Set", key, bytes)
	_, err = c.Do("expire", key, reqDur)
	if err != nil {
		fmt.Print(err)
		return
	}
}
func SetRedisWithoutExpired(bytes []byte, key string) {
	c := pool.Get()
	defer c.Close()
	_, err := c.Do("Set", key, bytes)
	if err != nil {
		return
	}
}
func GetRedis(id string) ([]byte, error) {
	connect := pool.Get()
	defer connect.Close()
	r, err := redis.Bytes(connect.Do("Get", id))
	return r, err
}
func EXISTS(id string) int {
	connect := pool.Get()
	defer connect.Close()
	r, _ := redis.Int(connect.Do("EXISTS", id))
	return r
}
